package com.test.services;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.test.comment.FightBean;
import com.test.comment.JudgeBean;
import com.test.comment.SingerBean;
import com.test.comment.SingerRaceBean;
import com.test.comment.UservoteBean;
import com.test.dao.UserDao;

@Service
public class ActiveAdminServices {
	Logger logger = Logger.getLogger(getClass());
     @Autowired
     UserDao userDao;
     @Transactional
	 public void insertfight(Integer raceid, Integer singerid1, Integer singerid2) {
		
		userDao.insertfigth(raceid, singerid1, singerid2);
		
	}
	public List<SingerRaceBean> singerrace(int tmp, Integer limit, Integer rid) {
		List<SingerRaceBean> singerRaceBeans = userDao.singerrace(tmp, limit, rid);
		for(int i=0;i<singerRaceBeans.size();i++) {
			String string = userDao.ex(singerRaceBeans.get(i).getRid(), singerRaceBeans.get(i).getSid());
			if(string!=null) {
				singerRaceBeans.get(i).setStatus(1);
			}
		}
		return singerRaceBeans;
	}
	public List<FightBean> racesinger(int tmp, Integer limit, Integer rid) {
		List<FightBean> fightBeans = userDao.allfight(tmp, limit, rid);
		logger.debug("=========::"+fightBeans.get(0).getFid());
	
		if(fightBeans!=null) {
			for(int i=0;i<fightBeans.size();i++) {
			
				SingerRaceBean singerRaceBeans1= userDao.achi(fightBeans.get(i).getSidOne(), rid);
				
				String name1 = userDao.SingerName(fightBeans.get(i).getSidOne());
				fightBeans.get(i).setJudgeScoreOne(singerRaceBeans1.getSingerJudgeScore());
				fightBeans.get(i).setTotalScoreOne(singerRaceBeans1.getSingerTotalScore());
				fightBeans.get(i).setUserScoreOne(singerRaceBeans1.getSingerUserScore());
				fightBeans.get(i).setSidNameOne(name1);
				
				SingerRaceBean singerRaceBeans2= userDao.achi(fightBeans.get(i).getSidTwo(), rid);
				String name2 = userDao.SingerName(fightBeans.get(i).getSidTwo());
				fightBeans.get(i).setJudgeScoreTwo(singerRaceBeans2.getSingerJudgeScore());
				fightBeans.get(i).setTotalScoreTwo(singerRaceBeans2.getSingerTotalScore());
				fightBeans.get(i).setUserScoreTwo(singerRaceBeans2.getSingerUserScore());
				fightBeans.get(i).setSidNameTwo(name2);
			}
		}
	
		
		
		return fightBeans;
	}
	public String updatefight(Integer fid, Integer rid) {
		String tString = userDao.findfight(rid);
		logger.debug("-------------"+tString);
		if(tString==null) {
			userDao.updatafight(fid);
			return "开启成功";
		}else {
			logger.debug("11111111111");
			return "一次只能开启一个";
		}
		
		
	}
	@Transactional
	public FightBean stopfight(Integer fid, Integer rid) {
		
		 userDao.stopfight(fid);
		
		FightBean fightBean = userDao.fb(fid);
		List<JudgeBean> judgeBeansone = userDao.findjudge(fightBean.getSidOne(),rid);
		int sin1 = userDao.finduser(fightBean.getSidOne(),rid);
		int sintotal1 = userDao.findusertotal(rid);
		
		List<JudgeBean> judgeBeansone2 = userDao.findjudge(fightBean.getSidTwo(),rid);
        int sin2 = userDao.finduser(fightBean.getSidTwo(),rid);
		int sintotal2 = userDao.findusertotal(rid);
		
		int t1=0;
		for(int i=0;i<judgeBeansone.size();i++) {
			t1= t1+Integer.parseInt(judgeBeansone.get(i).getSinger_score());
		}
		int judge = t1/judgeBeansone.size();
		DecimalFormat df = new DecimalFormat("0.00");
		logger.debug("============judge:"+judge);
		String userscore=df.format((float)sin1/sintotal1);
	
		double user  = Double.valueOf(userscore);
		int num1 = (int)(user*100);
	
		fightBean.setJudgeScoreOne(Integer.toString(judge));
		fightBean.setUserScoreOne(Integer.toString(num1));
		fightBean.setTotalScoreOne(Integer.toString(judge+num1));
		
		
        
		int t2=0;
		for(int i=0;i<judgeBeansone2.size();i++) {
			t2= t2+Integer.parseInt(judgeBeansone2.get(i).getSinger_score());
		}
		
		int judge2 = t2/judgeBeansone2.size();
		
		String userscore2 = df.format((float)sin2/sintotal2);
		double user2  = Double.valueOf(userscore2);
		int num2 = (int)(user2*100);
		
		fightBean.setJudgeScoreTwo(Integer.toString(judge2));
		fightBean.setUserScoreTwo(Integer.toString(num2));
		fightBean.setTotalScoreTwo(Integer.toString(judge2+num2));
		logger.debug("==========user"+user);
		logger.debug("==========num1:"+num1);
		logger.debug("==========user2"+user2);
		logger.debug("==========num2:"+num2);
		//计算玩的成绩插入表中保存
		
		
		SingerRaceBean singerRaceBean1 = new SingerRaceBean();
		singerRaceBean1.setRid(rid);
		singerRaceBean1.setSid(fightBean.getSidOne());
		singerRaceBean1.setSingerJudgeScore(Integer.toString(judge));
		singerRaceBean1.setSingerUserScore(Integer.toString(num1));
		singerRaceBean1.setSingerTotalScore(Integer.toString(judge+num1));
		
		SingerRaceBean singerRaceBean2 =new SingerRaceBean();
		singerRaceBean2.setRid(rid);
		singerRaceBean2.setSid(fightBean.getSidTwo());
		singerRaceBean2.setSingerJudgeScore(Integer.toString(judge2));
		singerRaceBean2.setSingerUserScore(Integer.toString(num2));
		singerRaceBean2.setSingerTotalScore(Integer.toString(judge2+num2));
		
		userDao.insertSingerRace(singerRaceBean1);
		userDao.insertSingerRace(singerRaceBean2);
		return fightBean;
	}
	public List<SingerBean> findfight(Integer rid, Integer limit, Integer rid2) {
		
		return null;
	}
}
